//
//  WBStatusesToolBar.swift
//  weibo
//
//  Created by yanghong on 15/12/12.
//  Copyright © 2015年 杨鸿. All rights reserved.
//

import UIKit

class WBStatusesToolBar: UIView {
    
    var retweetButton: UIButton?
    var commentButton: UIButton?
    var unlikeButton: UIButton?
    
    var statusViewModel: WBStatuedViewModel? {
        didSet{
          //设置数据
            retweetButton?.setTitle(statusViewModel?.retweetCountString, forState: .Normal)
            commentButton?.setTitle(statusViewModel?.commentCountString, forState: .Normal)
            unlikeButton?.setTitle(statusViewModel?.attitudeCountString, forState: .Normal)
    
        }
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func  setupUI() {
        //添加 三个 button
        retweetButton = creatToolBarButton("转发", imageName: "timeline_icon_retweet")
        commentButton = creatToolBarButton("评论", imageName: "timeline_icon_comment")
        unlikeButton = creatToolBarButton("赞", imageName: "timeline_icon_unlike")
        
        // 添加两个分割线
        
        let separator1 = addSepatator()
        let sepatator2 = addSepatator()
        
        //添加约束
        // retweetButton 的约束
        retweetButton?.snp_makeConstraints(closure: { (make) -> Void in
            
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        })
        // commentButton 的约束
        commentButton?.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton!)
            
        })
        // unlikeButton 的约束
        unlikeButton?.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
            make.trailing.equalTo(self)
        })
        //分割线 约束
        separator1.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetButton!.snp_trailing)
        }
        sepatator2.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentButton!.snp_trailing)
        }
    }
    // MARK -  创建button的方法
    
    private func creatToolBarButton(titile: String, imageName: String) -> UIButton {
        
        let button = UIButton()
        
        button.setTitle(titile, forState: .Normal)
        button.setTitleColor(UIColor.darkTextColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)
        button.setImage(UIImage(named: imageName), forState: .Normal)
        addSubview(button)
        return button
        
    }
    // MARK -  添加分割线
    
    private func addSepatator() -> UIImageView {
    
    let separator  = UIImageView()
        
        separator.image = UIImage(named: "timeline_card_bottom_line")
      addSubview(separator)
     return separator
    }
    
}
